// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

export enum moveDir {
    up = 0,
    down = 1,
    left = 2,
    right = 3
}

@ccclass
export default class cameraMove extends cc.Component {

    @property(cc.Node)
    endingNode : cc.Node = null;

    @property({
        type : cc.Enum(moveDir),
        displayName : "移动方向"
    })
    curDir : moveDir = moveDir.right;

    update (dt) {
        let pos  = cc.find("Canvas/role").convertToWorldSpaceAR(cc.v2(0,0));
        let pos2 = this.node.convertToNodeSpaceAR(pos);
        switch (this.curDir) {
            case moveDir.left :
                if(pos2.x < 0 && this.node.x - 5 >= (this.endingNode.x - this.endingNode.width / 2 + this.node.width /2)){
                    this.node.x -= 5;
                }
                if(pos2.x > this.node.width / 2 - 150 && this.node.x <= 0 ){
                    this.node.x += 5;
                }
                break;
            case moveDir.right :
                if(pos2.x > 0 && this.node.x + 5 <= (this.endingNode.x + this.endingNode.width / 2 - this.node.width /2)){
                    this.node.x += 5;
                }
                if(pos2.x < -this.node.width / 2 + 100){
                    this.node.x -= 5;
                }
                break;
            case moveDir.up:
                break;
            case moveDir.down :
                break;
        }

    }
}
